package com.network.update;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.network.retrofit_rx.Api.ActionSubscriber;
import com.network.retrofit_rx.Api.CommandImpl;
import com.network.retrofit_rx.Api.HttpManager;
import com.network.retrofit_rx.download.FileDownLoadProgressDialog;
import com.network.utils.NetworkUtils;

import io.reactivex.functions.Consumer;


/**
 * 软件升级
 *
 * @author QiQi
 * @date 2017/11/13
 */

public class SoftUpdate {
    /**
     * 请求升级结果
     *
     * @param context    上下文
     * @param command    请求的Url及参数对象
     * @param ft
     * @param updateInfo 返回解析升级信息
     * @param isTip      是否需要显示吐司
     */
    public static void checkUpdate(final Context context, CommandImpl command, final FragmentManager ft, final IGetUpdateInfo updateInfo, final boolean isTip) {
        HttpManager.getInstance().get(command, new ActionSubscriber<>(new SubscribeUpdate(context, updateInfo, ft, isTip), null, null));
    }

    private static class SubscribeUpdate implements Consumer<JSON> {

        private Context context;
        private IGetUpdateInfo updateInfo;
        private FragmentManager ft;
        private boolean isTip;

        SubscribeUpdate(Context context, IGetUpdateInfo updateInfo, FragmentManager ft, boolean isTip) {
            this.context = context;
            this.updateInfo = updateInfo;
            this.ft = ft;
            this.isTip = isTip;
        }

        /**
         * Consume the given value.
         *
         * @param jsonElement the value
         */
        @Override
        public void accept(JSON jsonElement) {
            boolean isUpgrade = updateInfo.isUpgrade(jsonElement);
            if (isUpgrade) {
                final String upgradeUrl = updateInfo.getUpdateUrl(jsonElement);
                if (TextUtils.isEmpty(upgradeUrl)) {
                    return;
                }
                final String message = updateInfo.getUpdateInfo(jsonElement);
                final String leftBtnText = "立即更新";
                final String rightText = "稍候更新";
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setMessage(message)
                        .setNegativeButton(leftBtnText, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog12, int which) {
                                dialog12.dismiss();
                                FileDownLoadProgressDialog progressDialog = new FileDownLoadProgressDialog();
                                progressDialog.setDownLoadUrl(upgradeUrl);
                                progressDialog.show(ft, "progress");
                            }
                        })
                        .setNeutralButton(rightText, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog1, int which) {
                                dialog1.dismiss();
                            }
                        }).create();
                dialog.setCancelable(false);
                dialog.show();
            } else {
                if (isTip) {
                    Toast.makeText(NetworkUtils.appContext, "当前已经是最新版本", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

}
